import store from '../vuex/store'
import helper from '../helper'

export default router => {
  router.map({
    '/login': {
      name: 'login',
      component: require('../components/user/login'),
      title: '登录',
      auth: false,
    },
    '/': {
      name: 'index',
      component: require('../components'),
      title: '首页',
    },
    '/orders': {
      name: 'orders',
      component: require('../components/orders'),
      title: '订单列表',
    },
    '/order/:id': {
      name: 'order',
      component: require('../components/order'),
      title: '订单',
    },
    '/coupons': {
      name: 'coupons',
      component: require('../components/coupons'),
      title: '优惠券列表',
    },
    '/coupons/list': {
      name: 'new',
      component: require('../components/coupons/new'),
      title: '批量优惠券列表',
    },
     '/coupons/channel': {
      name: 'channel',
      component: require('../components/coupons/channel'),
      title: '渠道优惠券列表',
    },
    '/coupon/:id': {
      name: 'coupon',
      component: require('../components/coupon'),
      title: '优惠券',
    },

    //Word文章
     '/articles': {
      name: 'articles',
      component: require('../components/articles'),
      title: 'Word文章列表',
    },
    '/articles/new': {
      name: 'new',
      component: require('../components/articles/new'),
      title: '创建Word文章',
    },
    '/articles/:id': {
      name: 'new',
      component: require('../components/articles/new'),
      title: '编辑Word文章',
    },

    '/article/:id': {
      name: 'show',
      component: require('../components/articles/show'),
      title: 'Word文章',
    },


    '/products': {
      name: 'products',
      component: require('../components/products'),
      title: '产品列表',
    },
    '/product/:id': {
      name: 'product',
      component: require('../components/product'),
      title: '产品',
    },
    '/users': {
      name: 'users',
      component: require('../components/users'),
      title: '用户列表',
    },
    '/user/:id': {
      name: 'user',
      component: require('../components/user'),
      title: '用户',
    },
    '/reports': {
      name: 'reports',
      component: require('../components/reports'),
      title: '验光报告列表',
    },
    '/report/:id': {
      name: 'report',
      component: require('../components/report'),
      title: '验光报告',
    },
    '/optometrists': {
      name: 'optometrists',
      component: require('../components/optometrists'),
      title: '验光师列表',
    },
    '/optometrist/:id': {
      name: 'optometrist',
      component: require('../components/optometrist'),
      title: '验光师',
    },
    '/msgs': {
      name: 'msgs',
      component: require('../components'),
      title: '消息列表',
    },
    '/settings': {
      name: 'settings',
      component: require('../components/settings'),
      title: '设置列表',
    },
    // '/project/create': {
    //   name: 'projectCreate',
    //   component: require('../components/projects/create/Index'),
    //   title: '新建项目',
    // },
    // '/project/:id': {
    //   name: 'project',
    //   component: require('../components/project/Index'),
    //   title: '项目',
    // },
    // '/projects': {
    //   name: 'projects',
    //   component: require('../components/projects/Index'),
    //   title: '项目列表',
    // },
    // '/vcs/': {
    //   name: 'vcs',
    //   component: require('../components/vcs/Index'),
    //   title: '投资人列表',
    // },
    // '/vc/:id': {
    //   name: 'vc',
    //   component: require('../components/vc/Index'),
    //   title: '投资人',
    // },

    // '/reset': {
    //   name: 'reset',
    //   component: require('../components/login/Reset'),
    //   title: '找回密码',
    // },
    // '/user/password': {
    //   name: 'user_password',
    //   component: require('../components/user/Password'),
    //   title: '修改密码',
    // },
    // '/user/recycle': {
    //   name: 'user_recycle',
    //   component: require('../components/user/Recycle'),
    //   title: '回收站',
    // },
    // '/admin': {
    //   name: 'admin',
    //   component: require('../components/admin/Index'),
    //   title: '团队成员帐号',
    // },
    // '/admin/entities': {
    //   name: 'entities',
    //   component: require('../components/admin/持股主体'),
    //   title: '持股主体',
    // },
    // '/admin/custom_titles': {
    //   name: 'custom_titles',
    //   component: require('../components/admin/自定义职位'),
    //   title: '自定义职位',
    // },
    // '/admin/custom_permissions': {
    //   name: 'custom_permissions',
    //   component: require('../components/admin/自定义权限组'),
    //   title: '自定义权限组',
    // },
    '/test': {
      name: 'test',
      component: require('../components/test'),
      title: '测试',
      auth: false,
    },
    '*': {
      name: 'notFound',
      component: require('../components/notFound'),
      title: '404 Not Found',
      auth: false,
    },
    // '/error': {
    //   name: 'error',
    //   component: require('../components/Error'),
    //   title: 'Error',
    // },
  })
  router.beforeEach(({ to: { auth, title, path }, next, redirect, abort }) => {
    for (var i = 1; i < 99999; i++){
      window.clearInterval(i)
    }
    const fn = () => {
      helper.title(title)
      if (auth !== false && !store.state.user.me.token) {
        notieError('请登录')
        redirect({name: 'login'})
      } else {
        next()
      }
    }
    try {
      fn()
    } catch (e) {
      abort()
      console.error(e)
      notieError('网页错误, 请联系管理员')
    }
  })

  // // normal routes
  // router.map({
  //   // basic example
  //   '/about': {
  //     // the component can also be a plain string component id,
  //     // but a component with that id must be available in the
  //     // App component's scope.
  //     component: require('./components/about.vue')
  //   },
  //
  //   // nested example
  //   '/user/:userId': {
  //     component: require('./components/user/index.vue'),
  //     subRoutes: {
  //       // matches "/user/:userId/profile/:something"
  //       'profile/:something': {
  //         component: require('./components/user/profile.vue')
  //       },
  //       // matches "/user/:userId/posts"
  //       'posts': {
  //         component: require('./components/user/posts.vue')
  //       },
  //       // matches "/user/:userId/settings"
  //       'settings': {
  //         component: require('./components/user/settings.vue')
  //       }
  //     }
  //   },
  //
  //   // not found handler
  //   '*': {
  //     component: require('./components/not-found.vue')
  //   },
  //
  //   // advanced example
  //   '/inbox': {
  //     component: require('./components/inbox/index.vue'),
  //     subRoutes: {
  //       '/message/:messageId': {
  //         component: require('./components/inbox/message.vue')
  //       },
  //       '/archived': {
  //         component: require('./components/inbox/archive.vue')
  //       },
  //       // default component to render into the nested outlet
  //       // when the parent route is matched but there's no
  //       // nested segment. In this case, "/inbox".
  //       '/': {
  //         // inline component
  //         component: {
  //           template: 'default yo'
  //         }
  //       }
  //     }
  //   }
  // })
  //
  // // redirect
  // router.redirect({
  //   '/info': '/about',
  //   '/hello/:userId': '/user/:userId'
  // })
  //
  // // global before
  // // 3 options:
  // // 1. return a boolean
  // // 2. return a Promise that resolves to a boolean
  // // 3. call transition.next() or transition.abort()
  // router.beforeEach((transition) => {
  //   if (transition.to.path === '/forbidden') {
  //     router.app.authenticating = true
  //     setTimeout(() => {
  //       router.app.authenticating = false
  //       alert('this route is forbidden by a global before hook')
  //       transition.abort()
  //     }, 3000)
  //   } else {
  //     transition.next()
  //   }
  // })
}
